#!/bin/sh
#*********************************************************************
#   Copyright (c) International Business Machines  Corp., 2000
#
#   This program is free software;  you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY;  without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
#   the GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program;  if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
#  FILE   : ssh
#
#  PURPOSE: Tests to see that ssh allows a valid username (non-root)
#
#  SETUP: The program `/usr/bin/expect' MUST be installed.
#
#  HISTORY:
#    03/03 Jerone Young (jeroney@us.ibm.com)
#
#  Filename:    ssh09
#  Version:     1.0
#  Date:        2013-5-13
#  Author:      Heihuijing
#  Email:       huijing.hei@cs2c.com.cn
#  Summary:     02系统基本功能-04网络服务-10SSH-04命令操作-01远程执行命令-03普通用户身份登录ssh，执行存在的命令
#               02系统基本功能-04网络服务-10SSH-04命令操作-01远程执行命令-04普通用户身份登录ssh，执行不存在的命令
#               02系统基本功能-04网络服务-10SSH-04命令操作-01远程执行命令-05普通用户身份登录ssh，执行没有被授权的命令
#  History:
#               Version 1.0, 2013-5-13
#               - ssh allows normal users to login and exec commands.

#-----------------------------------------------------------------------
# FUNCTION:  do_setup
#-----------------------------------------------------------------------

do_setup()
{
    RHOST=${RHOST}
    export TEST_USER="ssh_usr3"
    export TEST_USER_PASSWD="eal"
    export TEST_USER_ENCRYPTED_PASSWD="42VmxaOByKwlA"
    export TEST_USER_HOMEDIR="/home/$TEST_USER"

    #exists expect ssh useradd userdel

    ${RSH} -l root -n $RHOST "/usr/sbin/userdel -r $TEST_USER"
    sleep 1

    if ! ${RSH} -l root -n $RHOST "/usr/sbin/useradd -m -p $TEST_USER_ENCRYPTED_PASSWD $TEST_USER"; then
        end_testcase "Could not add test user $TEST_USER to system $RHOST."
    fi

    trap do_cleanup INT EXIT
}

#-----------------------------------------------------------------------
# FUNCTION:  do_cleanup
#-----------------------------------------------------------------------

do_cleanup()
{
    ${RSH} -l root -n $RHOST "/usr/sbin/userdel -r $TEST_USER"
    EchoInfo "delete user $TEST_USER"
}

#-----------------------------------------------------------------------
# FUNCTION:  MAIN
#
# DESCRIPTION: Create Test User
#              Call upon script to make sure an invalid user can not have access
#              Cleanup Test User from system
#              Exit with exit code of script called upon
#-----------------------------------------------------------------------
# include lib files
if [ -z "$SFROOT" ]
then
    CMDLIB=./
    SFROOT=./
else
    CMDLIB=${SFROOT}/testcases/Network_Service
fi

. ${SFROOT}/lib/Echo.sh
. ${CMDLIB}/lib/Net.sh
. ${SFROOT}/lib/XmlParse.sh

. ${CMDLIB}/lib/net_cmdlib.sh
. ${CMDLIB}/setup.sh

# get RHOST, RUSERNAME, PASSWORD from config.xml
DATA_FILE="${SFROOT}/testcases/Network_Service/config.xml"
[ -e $DATA_FILE ] || { echo "$DATA_FILE not find, test end."; exit 1; }

XmlParse ${DATA_FILE}

GetValue REHOST ReHost
GetValue RUSERNAME UserName
GetValue PASSWORD RootPasswd

export RHOST=${REHOST}
export RUSERNAME=${RUSERNAME}
export PASSWORD=${PASSWORD}


read_opts $*
setup_env || { echo "setup env failed, test end."; exit 1; }

do_setup
./ssh_user_exec_s1 || end_testcase "Testcase failed"
exit 0
